<template>
  <div class="content-box">
    <nav-title title="亲属信息" />
    <edit-line label="姓名" :important="true" v-model="fams.cName"></edit-line>
    <edit-option-line label="关系" :important="true" :vals="relationShipList" v-model="fams.cRelationFam"></edit-option-line>
    <edit-line label="工作地点" :important="true" v-model="fams.cLocationDescr"></edit-line>
    <edit-line label="身份证号" :important="true" v-model="fams.nationalId"></edit-line>
    <edit-line label="家庭地址" :important="true" v-model="fams.addressHome"></edit-line>
    <edit-line label="联系电话" :important="true" v-model="fams.cPhone"></edit-line>
    <edit-option-line label="是否本公司员工" :vals="ynList" v-model="fams.ourCompany"></edit-option-line>
    <edit-line label="本公司部门" v-model="fams.companyDescr"></edit-line>
    <edit-line label="本公司岗位" v-model="fams.companyPost"></edit-line>

    <div class="btn_region">
      <div class="submit-btn" @click="save">保存</div>
      <div class="remove-btn" @click="remove"> 删除 </div>
    </div>
    <div stype="width:100%;height:30px">&nbsp;</div>
  </div>

</template>

<script>
import { Datetime, Group, PopupRadio } from 'vux'
import CommonUtil from '../../../../components/basic/util'
import EditLine from '../../../../components/basic/EditLine'
import EditOptionLine from '../../../../components/basic/EditOptionLine'
import { saveFamily,delFamily,getDicItem } from '../../../../api/api'
import { json2Form } from '../../../../util/http'

export default {

  name: 'input-information',
  components: {
    EditLine,EditOptionLine, Datetime, Group, PopupRadio
  },
  mounted(){
    //获取字典
    CommonUtil.loadDic(this.relationShipList, 'C_RELATION_FAM', "xlatshortname","fieldvalue");//亲属关系
  },
  created() {
    this.getFams()
    if(this.$store.state.hid){
      this.fams.rsHireId = this.$store.state.hid
    }
  },
  methods: {
    save(){
      if(!CommonUtil.valid(this, this.fams, "cName","请输入姓名") 
        || !CommonUtil.valid(this, this.fams, "cRelationFam","请选择关系") 
        || !CommonUtil.valid(this, this.fams, "cLocationDescr","请输入工作地点") 
        || !CommonUtil.valid(this, this.fams, "nationalId","请输入身份证号") 
        || !CommonUtil.valid(this, this.fams, "addressHome","请输入家庭地址")
        || !CommonUtil.valid(this, this.fams, "cPhone","请输入联系电话") 
        ){
        return;
      }
      if(!CommonUtil.checkPhone(this.fams.cPhone)) {
        this.$vux.alert.show({
          title: '提示',
          content: '请输入正确的手机号',
        })
        return;
      }
      console.error(this.fams.nationalId)
      if(!CommonUtil.checkNationId(this.fams.nationalId)) {
        this.$vux.alert.show({
          title: '提示',
          content: '请输入正确的身份证号',
        })
        return;
      }
      const that = this;
      saveFamily(json2Form(this.fams)).then(res => {
        //成功：000000
        if(res && res.retCode === '000000' ){
          //保存成功
          // 显示
          this.$vux.alert.show({
            title: '提示',
            content: '保存成功',
            onHide () {
              that.$router.push('/informationCollection/userInfo');
            }
          })

        }else{
          // 显示
          this.$vux.alert.show({
            title: '提示',
            content: res.retMsg
          })
        }
      }).catch (res=>{
        this.$vux.alert.show({
          title: '警告',
          content: '网络异常，请稍后重试'
        })
      })
    },
    remove(){

      const that = this;
      that.$vux.confirm.show({
          // 组件除show外的属性
          title: '提示',
          content: '确定删除？',
          onCancel () {
          },
          onConfirm () {
            delFamily(json2Form(that.fams)).then(res=>{
              //成功：000000
              if(res && res.retCode === '000000' ){
                  that.$router.push('/informationCollection/userInfo');
              }else{
                // 显示
                that.$vux.alert.show({
                  title: '提示',
                  content: res.retMsg
                })
              }
            })
            .catch(res=>{
              that.$vux.alert.show({
                title: '',
                content: '网络异常，请稍后重试'
              })
            })
          }
        })

    },
    getFams() {
      if(this.$route.params.fam){
        this.fams = this.$route.params.fam
        console.log(this.fams)
      }
    }

  },
  data() {
    return {
      fams:{
        rsHireId: '',
      },
      relationShipList: [],
      ynList: [{value: '是', key: 'Y'},{value: '否', key: 'N'}],
    }
  }
}

</script>
<style type="text/scss" lang="scss" scoped>
@import "./editinfo";
</style>
